using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.XtraEditors;
using System.Reflection;
using Microsoft.Practices.CompositeUI;
using NetRosApp.Infrastructure.Interface;
using NetROS.Tiers.Entities;
using NetROS.Tiers.Data;


namespace NetRosApp.Infrastructure.Interface
{
   public static class TraziUtility2
    {

        //public virtual static Type GetViewForDokumentTip(int IDDokumentTip)
        //{
        //    Type EditorClass = null;
        //    switch (IDDokumentTip)
        //    {

        //        default:
        //            break;

        //    }
        //    return EditorClass;
        //}

        public static void AddBracketsAndShowInTextEdit(object p, TextEdit pTextEdit)
        {

            if (p != null)
            {
                string tmpstring = Convert.ToString(p);
                if (tmpstring.Length != 0)
                    pTextEdit.Text = "" + tmpstring + "*";
            }
        }
        public static void ShowInTextEdit(object p, TextEdit pTextEdit)
        {

            if (p != null)
            {
                string tmpstring = Convert.ToString(p);
                if (tmpstring.Length != 0)
                    pTextEdit.Text = tmpstring;
            }
        }

        public static string AddBrackets(string p)
        {
            string tmpstring = "";
            if (p != null)
            {
                tmpstring = Convert.ToString(p);
              //  if (tmpstring.Length != 0)
                   // tmpstring = "" + tmpstring + "\"";
            }
            return tmpstring;
        }
        public static string AddBracketsStar(string p)
        {
            string tmpstring = "";
            if (p != null)
            {
                tmpstring = Convert.ToString(p);
                if (tmpstring.Length != 0)
                    tmpstring = "" + tmpstring + "*";
            }
            return tmpstring;
        }



        public static string GetParametersForEntity<EntityType, ParameterBuilderType>(object servis, object value, string NazivKolone)
        {
            if ((value as String) != null && (value as String).Length == 0) return string.Empty;
            object ParameterBuidler = Activator.CreateInstance(typeof(ParameterBuilderType));

            String foundBankaResult = "\" \"";

            System.Collections.IEnumerable _foundBankaList;

            MethodInfo append = ParameterBuidler.GetType().GetMethod("Append", new Type[] { typeof(string), typeof(string) });

            object[] mParam = new object[] { NazivKolone, value };

            append.Invoke(ParameterBuidler, mParam);

            object foundparamet = ParameterBuidler.GetType().GetMethod("GetParameters").Invoke(ParameterBuidler, null);

            object[] mParamServis = new object[] { foundparamet };

            MethodInfo FindService = servis.GetType().GetMethod("Find", new Type[] { typeof(SqlFilterParameterCollection) });

            _foundBankaList = FindService.Invoke(servis, mParamServis) as System.Collections.IEnumerable;

            foreach (object _foundBanka in _foundBankaList)
            {
                if (_foundBanka.GetType() == typeof(EntityType))
                {
                    foundBankaResult += ", " + TraziUtility2.AddBrackets(_foundBanka.GetType().GetProperty("ID").GetValue(_foundBanka, null).ToString());
                }
            }
            return foundBankaResult.ToString();
        }



        static public object getobject(object p, string field)
        {
            PropertyInfo property = p.GetType().GetProperty(field);
            try
            {
                return property.GetValue(p, null);
            }

            catch
            {
                return null;
            }
        }

        static public object getUnboundValue(object p, string[] field)
        {
            foreach (string tmpstr in field)
            {
                if (p != null)
                {
                    p = getobject(p, tmpstr);
                }
                else
                {
                    return String.Empty;
                }

            }
            if (p != null) return p;

            return String.Empty;
        }

        static public object getUnboundValue(object p, string value)
        {
            string[] tmp = value.Split('.');
            return getUnboundValue(p, tmp);
        }

        static public void setUnboundValue(object p, string field, object p_3)
        {
            PropertyInfo property = p.GetType().GetProperty(field);
            property.SetValue(p, p_3, null);
        }

    }
}
